package com.jackie.tongzhuang.Controls;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jackie.tongzhuang.R;

public class TitleBar extends LinearLayout implements View.OnClickListener {

    //定义控件
    private ImageView titlebarback,titlebarsubmit;
    private TextView titlebartext;
    private LinearLayout titlebarlayout;

    private String title_text;
    private Drawable back_drawable,submit_drawable;
    private int layout_color,text_color;
    private int text_size;
    private boolean hide_back,hide_submit;

    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        View view = LayoutInflater.from(context).inflate(R.layout.control_titlebar,this);
        titlebarback = view.findViewById(R.id.titlebar_back);
        titlebarsubmit = view.findViewById(R.id.titlebar_submit);
        titlebartext = view.findViewById(R.id.titlebar_text);
        titlebarlayout = view.findViewById(R.id.titlebar_layout);
        titlebarback.setOnClickListener(this);
        titlebarsubmit.setOnClickListener(this);

        TypedArray t = context.obtainStyledAttributes(attrs,R.styleable.TitleBar);
        title_text = t.getString(R.styleable.TitleBar_titleText);
        back_drawable = t.getDrawable(R.styleable.TitleBar_backSrc);
        submit_drawable = t.getDrawable(R.styleable.TitleBar_submitSrc);
        layout_color = t.getColor(R.styleable.TitleBar_backgroundColor,0);
        text_color = t.getColor(R.styleable.TitleBar_textColor,0);
        text_size = t.getDimensionPixelSize(R.styleable.TitleBar_TextSize,14);
        hide_back = t.getBoolean(R.styleable.TitleBar_hideBack,false);
        hide_submit = t.getBoolean(R.styleable.TitleBar_hideSubmit,false);

        if(title_text != null){
            titlebartext.setText(title_text);
        }

        if(layout_color != 0){
            titlebarlayout.setBackgroundColor(layout_color);
        }

        if(text_color != 0){
            titlebartext.setTextColor(text_color);
        }

        if(back_drawable != null){
            titlebarback.setImageDrawable(back_drawable);
        }

        if(submit_drawable != null){
            titlebarsubmit.setImageDrawable(submit_drawable);
        }

        if(text_size != 0){
            titlebartext.setTextSize(text_size);
        }

        if(hide_back){
            titlebarback.setVisibility(View.GONE);
        }
        else{
            titlebarback.setVisibility(View.VISIBLE);
        }

        if(hide_submit){
            titlebarsubmit.setVisibility(View.GONE);
        }else{
            titlebarsubmit.setVisibility(View.VISIBLE);
        }

        t.recycle();

    }

    public void setText(String str){
        titlebartext.setText(str);
    }

    public void setBackResources(int resid){
        titlebarback.setImageResource(resid);
    }

    public void setSubmitResources(int resid){
        titlebarsubmit.setImageResource(resid);
    }

    public void setBackgroundColor(int color){
        titlebarlayout.setBackgroundColor(color);
    }

    public void setTextColor(int color){
        titlebartext.setTextColor(color);
    }

    public void setTextSize(int size){
        titlebartext.setTextSize(size);
    }

    public void setHideBack(boolean bool){
        if(bool){
            titlebarback.setVisibility(View.GONE);
        }
        else{
            titlebarback.setVisibility(View.VISIBLE);
        }
    }

    public void setHideSubmit(boolean bool){
        if(bool){
            titlebarsubmit.setVisibility(View.GONE);
        }
        else{
            titlebarsubmit.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.titlebar_back:
                listener.backClick(view);
                break;
            case R.id.titlebar_submit:
                listener.submitClick(view);
                break;
        }
    }

    /* 定义按钮点击接口，实现回调机制，通过映射的接口对象调用接口中的方法
     * 而不用去考虑如何实现，具体实现由调用者去创建
     */
    public interface TitleBarClickListener{
        void backClick(View v);   //左侧按钮点击事件
        void submitClick(View v);  //右侧按钮点击事件
    }

    public TitleBarClickListener listener;

    public void setOnClickedListener(TitleBarClickListener _listener){
        listener = _listener;
    }
}
